using Autofac;
using Autofac.Integration.Mvc;
using AutofacProject.App_Start;
using AutofacProject.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace AutofacProject
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            //auto配置
            AutoMapperConfig.Config();
            //fac配置调用
            autofac();

           
        }

        //fac配置
        public static void autofac()
        {
            var builder = new ContainerBuilder();
            //2.依赖注入当前应用程序下的所有Controller
            builder.RegisterControllers(typeof(MvcApplication).Assembly);
            builder.RegisterType<KeDa2024DbContext>();
            Assembly asmService = Assembly.Load("AutofacProject.Service");
            builder.RegisterAssemblyTypes(asmService).Where(t => !t.IsAbstract).AsImplementedInterfaces().PropertiesAutowired();

            Assembly asmRepository = Assembly.Load("AutofacProject.Repository");
            builder.RegisterAssemblyTypes(asmRepository).Where(t => !t.IsAbstract).AsImplementedInterfaces().PropertiesAutowired();
            var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
    }
}
